﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SayisalAnaliz.Library
{
    public static class GaussEleminasyonYontemi
    {
        private static double[,] MatrisOku(string matrisMetin)
        {
            double[,] matris = null;
            string[] satirlar = matrisMetin.Split(new string[] { Environment.NewLine , "\n" }, StringSplitOptions.None);
            matris = new double[satirlar.Length, satirlar.Length];

            for (int i = 0; i < satirlar.Length; i++)
            {
                string[] sutunlar = satirlar[i].Split(null);

                if (sutunlar.Length > satirlar.Length)
                    return null;

                for (int j = 0; j < sutunlar.Length; j++)
                {
                    matris[i, j] = Convert.ToDouble(sutunlar[j]);

                }
            }
            return matris;
        }
        public static List<double> Uygula(string katsayilar,string sonuclar)
        {
            List<double> cozumler = new List<double>();
            string[] sonuc_liste = sonuclar.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            double[] B = new double[sonuc_liste.Length];
            for (int i = 0; i < sonuc_liste.Length; i++)
            {
                B[i] = Convert.ToDouble(sonuc_liste[i]);
            }

            double[,] A = MatrisOku(katsayilar.Replace("\r", ""));          
            double bolum;
            double[] bolum_dizi = new double[3];
            

            int n = B.Length;
                     
            
            for (int i = 0; i < n; i++)
            {
                bolum = A[i,i];
                for (int j = 0; j < n; j++)
                {
                    A[i,j] = A[i,j] / bolum;
                }
                B[i] = B[i] / bolum;

                for (int j = 0; j < n; j++)
                {
                    bolum_dizi[j] = A[j,i];
                }
                for (int k = 0; k < n; k++)
                {
                    if (i != k)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            A[k,j] = A[k,j] - A[i,j] * bolum_dizi[k];

                        }
                        B[k] = B[k] - B[i] * bolum_dizi[k];
                    }
                }
            }//end for
            
            for (int i = 0; i < n; i++)
            {
                cozumler.Add(B[i]);          
            }
            return cozumler;
        }
    }
}
